import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Image Texture'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final MethodChannel _channel = MethodChannel('image_texture');
  bool hasLoadTexture = false;
  int mainTexture = -1;

  @override
  void initState() {
    super.initState();

    newTexture();
  }

  void newTexture() async {
    mainTexture = await _channel.invokeMethod('newTexture');
    setState(() {
      hasLoadTexture = true;
    });
  }

  Widget buildWidget(BuildContext context) {
    if (!hasLoadTexture) return Text('loading...');

    return Container(
      width: 300,
      height: 300,
      decoration: BoxDecoration(
          border: Border.all(
        color: Colors.red,
        width: 1,
      )),
      child: Texture(textureId: mainTexture),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: buildWidget(context),
      ),
    );
  }
}
